package com.doyoung.appicker.view;

import java.util.ArrayList;
import java.util.List;




import java.util.HashMap;

import com.doyoung.appicker.R;
import com.doyoung.appicker.controller.AppController;
import com.doyoung.appicker.data.AppInfo;

import android.support.v4.app.Fragment;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

public class MainListFragment2 extends Fragment{
	
	private static MainListFragment2 INSTANCE;
	
	public static MainListFragment2 getInstance(){
		if (INSTANCE == null){
			INSTANCE = new MainListFragment2();
		}
		return INSTANCE;
	}
	
	private boolean initFlag = false;
	List<ApplicationInfo> appList = null;
	final ArrayList<HashMap<String, String>> viewList = new ArrayList<HashMap<String, String>>();
	final ArrayList<HashMap<String, Bitmap>> imgList = new ArrayList<HashMap<String, Bitmap>>();
	
	public static final String KEY_ID = "id";
	public static final String KEY_TITLE = "title";
	public static final String KEY_ARTIST = "artist";
	public static final String KEY_DURATION = "duration";
	public static final String KEY_THUMB_URL = "thumb_url";
	
	private String TAG = "MainListFragment2";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "MainListFragment2 onCreateView() Called");
		
		View view = inflater.inflate(R.layout.fragment_main_list2, container, false);
		final ListView listview = (ListView)view.findViewById(R.id.main_listView2);
		
		if (!initFlag){
			initFlag = true;
			
//			final ArrayList<HashMap<String, String>> songList = new ArrayList<HashMap<String, String>>();
			
			final PackageManager pm = getActivity().getPackageManager();
			appList = pm.getInstalledApplications(PackageManager.GET_META_DATA);
			
			for (ApplicationInfo appInfo : appList){
				HashMap<String, String> map = new HashMap<String, String>();
				HashMap<String, Bitmap> img = new HashMap<String, Bitmap>();
				BitmapDrawable drawable = (BitmapDrawable) pm.getApplicationIcon(appInfo);//appInfo.loadIcon(pm);
				
				map.put(KEY_ID, "");
				map.put(KEY_TITLE, pm.getApplicationLabel(appInfo).toString());
				map.put(KEY_ARTIST, appInfo.packageName);
				map.put(KEY_DURATION, "4:47");
				img.put(KEY_THUMB_URL, drawable.getBitmap());
//				map.put(KEY_THUMB_URL, "http://api.androidhive.info/music/images/adele.png");
				
				viewList.add(map);
				imgList.add(img);
			}
		}

		final AppController ac = new AppController(getActivity(), viewList, imgList);

		listview.setAdapter(ac);
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, final View view,
					int position, long id) {
				final int pos = position;

				view.animate().setDuration(1000).alpha(0).withEndAction(new Runnable() {
					@Override
					public void run() {
						if (pos < viewList.size()){
							viewList.remove(pos);
							imgList.remove(pos);
							ac.notifyDataSetChanged();
							view.setAlpha(1);
						}
						else{
							Log.d(TAG, "pos : " + pos + " < songList.size():" + viewList.size());
						}
					}
				});
			}
		});

		return view;
	}
}
